﻿using System;

namespace PureMVC.Other
{
    /// <summary>
    /// 延时创建工具类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class WeakLazy<T> 
    {
        bool isValueCreated = false;
        public bool IsValueCreeated
        {
            get { return isValueCreated; }
        }

        T value;
        public T Value
        {
            get
            {
                if (isValueCreated)
                {
                    return value;
                }

                if (valueFactory != null)
                {
                    value = valueFactory();
                }
                else
                {
                    value = default(T);
                }
                isValueCreated = true;
                return value;
            }
        }

        Func<T> valueFactory = null;

        public WeakLazy()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_factory"> T 的工厂方法</param>
        public WeakLazy(Func<T> _factory)
        {
            valueFactory = _factory;
        }
    }
}